package com.zwkj.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zwkj.common.R;
import com.zwkj.domain.WFileT;
import com.zwkj.domain.WFileVersionT;
import com.zwkj.pojo.vo.FileRequestVo;
import com.zwkj.pojo.vo.FileVo;
import com.zwkj.pojo.vo.WFileTVo;
import com.zwkj.pojo.vo.WFileVersionTVo;
import com.zwkj.service.IWFileTService;
import com.zwkj.service.IWFileVersionTService;
import com.zwkj.util.MinioFileUploadUtil;
import com.zwkj.util.RequestUtils;
import io.minio.MinioClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

@RestController
@RequestMapping("/")
public class EditWpsController {

    @Autowired
    private IWFileVersionTService wFileVersionTService;

    @Autowired
    private IWFileTService wFileTService;

    @Autowired
    private MinioFileUploadUtil minioFileUploadUtil;

    @PostMapping(value = "v3/3rd/files/{file_id}/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<WFileVersionTVo> uploadFile(@PathVariable("file_id") String fileId,
                                         @ModelAttribute FileRequestVo request) {
        String userId = RequestUtils.getWebOfficeToken(RequestUtils.getCurrentRequest());
        MultipartFile file = request.getFile();
        WFileT wFileT = wFileTService.getById(fileId);
        Integer versionNew = wFileT.getVersion() + 1;
        WFileT updateWFileT = new WFileT();
        updateWFileT.setVersion(versionNew);
        QueryWrapper update = new QueryWrapper();
        update.eq("id", fileId);
        wFileTService.update(updateWFileT, update);

        WFileVersionT wFileVersionT = new WFileVersionT();
        wFileVersionT.setFileId(Long.valueOf(fileId));
        wFileVersionT.setVersion(versionNew);
        wFileVersionT.setName(wFileT.getName());
        wFileVersionT.setSize((long) request.getSize());
        wFileVersionT.setCreateTime(LocalDateTime.now());
        wFileVersionT.setCreator(userId);
        wFileVersionT.setModifier(userId);
        wFileVersionT.setModifyTime(LocalDateTime.now());
        wFileVersionT.setDownloadUrl(wFileT.getDownloadUrl());
        wFileVersionTService.save(wFileVersionT);


        minioFileUploadUtil.minioUrlUpload(file, wFileT.getDownloadUrl());
        WFileVersionTVo wFileVersionTVo = new WFileVersionTVo();
        BeanUtils.copyProperties(wFileVersionT, wFileVersionTVo);
        wFileVersionTVo.setCreateTime(Math.toIntExact(wFileT.getCreateTime().toEpochSecond(ZoneOffset.UTC)));
        wFileVersionTVo.setModifyTime(Math.toIntExact(wFileT.getModifyTime().toEpochSecond(ZoneOffset.UTC)));
        wFileVersionTVo.setId(Long.valueOf(fileId));
        wFileVersionTVo.setCreatorId(userId);
        wFileVersionTVo.setModifierId(userId);
        return R.ok(wFileVersionTVo);
    }

}
